/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rnavizexperimentation;

/**
 *
 * @author Elias la Cour
 */
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class RotateEx2 extends JPanel {


    public char[] logic() {
        String s = "111111111111111111111111111111";
        long startT = System.currentTimeMillis();
        InputHandler i = new InputHandler();
        //SequenceInfo si = i.createSeq10000();
        Structure si = i.smallInput();
        char [] seq = si.getBaseSequence();
        long endT = System.currentTimeMillis();
        //System.out.println("createseq = " + (endT - startT));
        return seq;
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //sets font to be used
        Font font = new Font("Courier", Font.PLAIN, 12);

        //sets origin (starting point) of g2D to the given coord in a new context
        g2d.translate(20, 20);

        //needed for the GlyphVector class, contains info font size etc.
        FontRenderContext frc = g2d.getFontRenderContext();

        /**
         *contains the context for tranforming glyphs, methods in this class outputs
         * shape objects
         * @createGlyphVector - takes FontRenderContext and str, char[] or chariterator
         * should we use char[]?
         */
        char[] seq=logic();
        
        long startTc = System.currentTimeMillis();
        GlyphVector gv = font.createGlyphVector(frc, seq);
        int length = gv.getNumGlyphs();
        for (int i = 0; i < length; i++) {
            /**Point2D is a coord class contain x, y positions
             */
            Point2D p = gv.getGlyphPosition(i);

            /**
             * context between new coords and old coords. Special handling of 90 degree
             * rotations
             * returns a 3x3 matrix
             * at.rotate handles rotation
             */
            AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());

            at.rotate((double) i / (double) (length - 1) * Math.PI / 3);
            //at.rotate(Math.PI);
            
            //retrieves "letter to rotate
            Shape glyph = gv.getGlyphOutline(i);
            //imposes matrix for rotation and creates the new rotated shape
            Shape transformedGlyph = at.createTransformedShape(glyph);
            //draws new shape
            g2d.fill(transformedGlyph);
        }
            long endTc = System.currentTimeMillis();
            System.out.println("Tc = " + (endTc - startTc));
        
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("Rotated text");
        frame.add(new RotateEx2());
        frame.setSize(1200, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}
